//
//  CHMajorProjectViewController.m
//  SZTZ
//
//  Created by 陈浩 on 2017/3/6.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHMajorProjectViewController.h"

@interface CHMajorProjectViewController ()<CHPopViewDelegate>
@property (nonatomic ,strong) NSMutableArray *datas;

@property (nonatomic ,strong) CHItemButton *itemBtn;
@property (nonatomic ,assign) BOOL isReload;
@end

@implementation CHMajorProjectViewController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _isReload = NO;
    self.itemBtn = [[CHItemButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.itemBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.itemBtn setImage:[UIImage imageNamed:@"pull"] forState:UIControlStateNormal];
    //    [self.itemBtn setTitle:@"2017年12月" forState:UIControlStateNormal];
    [self.itemBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.itemBtn];;
    
    

}


-(void)loadData{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"type"] = @"fgwoa_sjtj_zdxm";
    [[Globle getInstance].service RequestWithServiceName:@"fgwoa_getVersionByState" params:params httpMethod:@"GET" resultIsDictionary:true completeBlock:^(id result) {
        
        if (result) {
            [self.datas removeAllObjects];
            for (NSMutableDictionary *dict in result) {
                CHDateModel *model = [CHDateModel objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            CHDateModel *model = self.datas[0];
            NSString *title = [NSString stringWithFormat:@"%@年%@月",model.nf,model.yf];
            [self.itemBtn setTitle:title forState:UIControlStateNormal];
            [self refreshDataWithModel:model];
        }else{
            
            [self.webView removeFromSuperview];
            [self showNotice];
        }
        _isReload = YES;
        
    }];
}

-(void)rightBtnClick:(CHItemButton *)btn{
    
    
    CGFloat popViewH = 200;
    
    if (self.datas.count * 44 < 200) {
        popViewH = self.datas.count * 44;
    }
    
    CHPopView *popView = [[CHPopView alloc] initWithFrame:CGRectMake(UIScreenW - 150 -10, 74, 150, popViewH)];
    popView.delegate = self;
    popView.layer.cornerRadius = 5;
    popView.layer.masksToBounds = YES;
    popView.isSection = NO;
    popView.datas = self.datas;
    [popView presentPopViewToView:btn];

}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    if (!_isReload) {
        [self loadData];
        //隐藏
        [self.tabBarController.tabBar hideBadgeOnItemIndex:2];
    }
    
}

-(void)popViewDidSelectRowAtIndex:(NSInteger)index{
    
    
    self.progressLayer = [WYWebProgressLayer new];
    self.progressLayer.frame = CGRectMake(0, 42, SCREEN_WIDTH, 2);
    
    [self.navigationController.navigationBar.layer addSublayer:self.progressLayer];
    
    CHDateModel *model = self.datas[index];
    NSString *title = [NSString stringWithFormat:@"%@年%@月",model.nf,model.yf];
    [self.itemBtn setTitle:title forState:UIControlStateNormal];
    
    [self refreshDataWithModel:model];
}

-(void)refreshDataWithModel:(CHDateModel *)model{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"type"] =@"fgwoa_sjtj_zdxm";
    params[@"nf"] = model.nf;
    params[@"yf"] = model.yf;
    params[@"username"] =  [CHUUIDManager readUserName];
    params[@"iosversion"] = @"8";
    [[Globle getInstance].service RequestWithServiceName:@"fgwoa_browseHistory" params:params httpMethod:@"POST" resultIsDictionary:false completeBlock:^(id result) {
        
        if (result) {
            NSString *serviceName = ServiceURL;
            NSString *urlStr = [serviceName stringByAppendingString:result];
             NSURL *url = [[NSURL alloc]initWithString:[urlStr stringByAppendingString:@"&iosversion=8"]];
            [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
            
        }
    }];
}


@end
